package com.estar.integaration.autojob;

import com.estar.integaration.Service.IMaterialService;
import com.estar.integaration.cache.AppRedisCacheManager;
import com.estar.integaration.entity.Material;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import groovy.util.logging.Slf4j;
import net.sf.cglib.core.Local;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class CacheJob {
    Logger log = LoggerFactory.getLogger(CacheJob.class);

    @Autowired
    IMaterialService materialService;
    AppRedisCacheManager redisCacheManager = new AppRedisCacheManager();

    @XxlJob("cacheJob")
    public ReturnT<String> cacheJob(String params){
        //更新缓存
        log.info("更新缓存");
        //查询material数据库近一个月的数据
        log.info("查询material数据库近一个月的数据");
        //查询mysql数据库
        //TODO 找到一个月内的数据
        List<Material> thisMonthMaterial = materialService.selectListByDateOneMonth();
        //TODO 更新缓存
        for(Material material:thisMonthMaterial){
            if(material.getMaterialNo() != null  && material.getMaterialName() != null){
                redisCacheManager.set(material.getMaterialNo(),material.getMaterialName(),24*60*60);
            }
        }
        log.info("写入一个月内的所有新数据");
        return ReturnT.SUCCESS;
    }
}
